import requests
import json

request_url = 'https://api.weixin.qq.com/sns/jscode2session'


def getOpenid(jscode):
    data = {
        # 'appid': 'wx8ca82daaa201257c',
        # 'secret': '2447c8658b3ecce03c9a709af1860090',

        # 企业接口
        'appid': 'wxe8968f1ed9d0bb49',
        'secret': '6b4440a65b1d3f6196de36cf38204b7b',
        'js_code': jscode,
        'grant_type': 'authorization_code'
    }

    res = requests.get(
        url=request_url,
        params=data
    )
    # res.text获取请求到的字典类型字符串，转换成字典类型，再通过键取出
    openid = json.loads(res.text)['openid']

    return openid
